#!/bin/bash
#Testscript run on metsähovi machines with vlbi-command
#Tests daemon-mode
VLBIPREFIX="/home/kharn/metsahovibin/vlbi"
end_streamer()
{
TIMEOUT=10 
$VLBIPREFIX vbs_shutdown
while [ "$($VLBIPREFIX pidof vlbistreamer)" ]
do
  echo "Waiting for vlbistreamer to stop"
  TIMEOUT=$(($TIMEOUT-1))
  if [ "$TIMEOUT" -le "0" ]
  then
    $VLBIPREFIX killall vlbistreamer
    echo "VLBIstreamer had to be killed!"
  else
    sleep 1
  fi
done
$VLBIPREFIX vbs_clearschedule
}

REMOTESENDER="watt.vlbi.metsahovi.fi"
LOCALRECEIVER="ara.vlbi.mestahovi.fi"
TESTTIME=30
WAIT=20
while [ $# -gt 0 ]
do
  case $1 in
    -f) 
      PLOTPARSED_EXTRA="-f $2"
      if [ ! -d "$2" ]
      then
	mkdir $2
      fi
      ROOTFOLDER=$2
      shift
      ;;
    -t)
      TESTTIME=$2
      shift
      ;;
    -w)
      WAIT=$2
      shift
      ;;
  esac
  shift
done
CFGFILE=/usr/local/etc/vlbistreamer.conf
SCHEDULE=/usr/local/var/opt/vlbistreamer/schedule
#LOGFILE=/usr/local/var/log/vlbistreamer.log
PARSELONG=3
PARSENORMAL=2
PACKETSIZE=`$VLBIPREFIX "cat $CFGFILE |grep packet_size |cut -d ' ' -f3 |head -c -$PARSELONG"`
PORT=`$VLBIPREFIX "cat $CFGFILE |grep port |tail -n 1|cut -d ' ' -f3 |head -c -$PARSENORMAL"`
TARGET="192.168.0.1"
OTARGET="192.168.0.2"
#ah gawd
VBS_START="$VLBIPREFIX sudo chrt -f -p 60 sudo -u oper src/vlbistreamer"
VBS_SEND="$VLBIPREFIX vbs_send"
VBS_RECV="$VLBIPREFIX vbs_record"
DRIVES=`$VLBIPREFIX "cat $CFGFILE |grep n_drives |cut -d ' ' -f3 |head -c -$PARSENORMAL"`
UDPPRE="cd /home/kharn/sshfs/${REMOTESENDER}/home/oper"
UDPBIN="/home/oper/src/udpmon_extra-1.3.1.VLBI/udpmon_send"
UDPRECV="/home/oper/src/udpmon_extra-1.3.1.VLBI/udpmon_recv"
UDPEND="cd -"
UDPOPTS=-"d $TARGET -p $PACKETSIZE -t $((TESTTIME-4)) -u $PORT -S 16777216 -w $WAIT"
UDPOPTSRECV="-t $(($TESTTIME+25)) -S 16777216 -u $PORT"
MAXMEM=`$VLBIPREFIX "cat $CFGFILE |grep maxmem |cut -d ' ' -f3 |head -c -$PARSELONG"`
METHODS=(def aio splice)
#VERBOSE=" > /dev/null"
EXTRA_PARAMS="$*"
DEFAULT_PARAMS="-A $MAXMEM -s $PORT -d $DRIVES"
TEMPLOG=thistemplogsurelyshouldntbeoverwritten
#TEMPLOG=$LOGFILE

$VLBIPREFIX vbs_clearschedule
echo "Cleaning up leftovers from previous test"
for i in ${METHODS[@]}
do
$VLBIPREFIX "vbs_delete testfor_${i}"
done

echo "setting up daemon mode"
$VLBIPREFIX ./configure --enable-user=oper --enable-logtofile=no --enable-ppriority=yes &> /dev/null
$VLBIPREFIX make &> /dev/null

if [ $? -ne "0" ]
then
  echo "Error in compilation"
  exit -1
fi

NRHUGE=`$VLBIPREFIX "cat /proc/sys/vm/nr_hugepages|head -c -1"`
if [ "$NRHUGE" -ne "0" ]
then
  DEFAULT_PARAMS="$DEFAULT_PARAMS -u "
fi

#echo "Emptying old log"
#echo " " > $LOGFILE

echo "Testing sending with nonexisting files"
for i in "${METHODS[@]}"
do
  $VBS_START -w $i &> $TEMPLOG &
  $VBS_SEND bogustuffthatshoulntexist $OTARGET
  sleep 2
  $VLBIPREFIX ps -A |grep  vlbistreamer &> /dev/null
  if [ "$?" -ne "0" ]
  then
    echo status $?
    echo "Bogus send crashed vlbistreamer!"
    cat $TEMPLOG
    end_streamer
    exit -1
  fi
  $VLBIPREFIX cat $TEMPLOG |grep ERROR &> /dev/null
  if [ "$?" -ne "0" ]
  then
    echo "Bogus send didnt output error!"
    cat $TEMPLOG
    end_streamer
    exit -1
  fi
  $VLBIPREFIX vbs_shutdown
  sleep 1
  $VLBIPREFIX ps -A |grep vlbistreamer &> /dev/null
  if [ "$?" -eq "0" ]
  then
    echo "vlbistreamer didnt shut down!"
    end_streamer
    exit -1
  fi
  echo -e ". $i\t\tok"
done

echo -e "Nonexisting files test ... \tOK"

DATE=$(date +%s)

echo -e "Testing send/receive"
for i in "${METHODS[@]}"
do
  TARGETFOLDER=${ROOTFOLDER}/${i}
  mkdir $TARGETFOLDER
  echo "Testing $i for receive"


  STATLOG=${TARGETFOLDER}/statlog
  #echo " " > $LOGFILE
  MAINLOG=${TARGETFOLDER}/mainlog
  #MAINLOG=$LOGFILE
  RECNAME=testfor_${i}
  #$VBS_START -w $i &> ${TEMPLOG}${i} &
  $VBS_START -w $i &> $MAINLOG &

  $VBS_RECV $RECNAME $TESTTIME
  nohup $VLBIPREFIX "scripts/statlogger $(($TESTTIME*2+10)) > $STATLOG " &
  $UDPPRE
  $VLBIPREFIX "$UDPBIN $UDPOPTS > udpmonlog" &
  echo "Receiving started.."
  sleep $(($TESTTIME +5))
  echo "..Receiving finished!"
  SENTPACKAGES=`cat udpmonlog | tail -n 1| awk '{print $2}'|head -c -2`
  $UDPEND
  RECVPACKAGES=`cat $MAINLOG | grep Packets:| awk '{print $2}'|head -c -1`

  if [ "$SENTPACKAGES" -ne "$RECVPACKAGES" ]
  then
    echo "Mismatch in received/sent files. received $(($SENTPACKAGES-$RECVPACKAGES)) less than sent"
  else
    echo "Same amount of packets sent and received"
  fi
  $VBS_SEND $RECNAME $OTARGET -a $WAIT
  $UDPPRE
  echo "Sending started.."
  TEMPVAR="$($VLBIPREFIX $UDPRECV $UDPOPTSRECV)"
  RECVPACKAGES=`echo $TEMPVAR|tail -n 1 |cut -d' ' -f5 |head -c -2`
  #sleep $(($TESTTIME +5))
  echo "..Sending finished!"
  #RECVPACKAGES=`cat udpmonlog | tail -n 1| awk '{print $5}'|head -c -2`
  $UDPEND
  SENTPACKAGES=`cat $MAINLOG | grep Packets:|tail -n1 | awk '{print $2}'|head -c -1`
  if [ "$SENTPACKAGES" -ne "$RECVPACKAGES" ]
  then
    echo "Mismatch in received/sent files. received $(($SENTPACKAGES-$RECVPACKAGES)) less than sent"
  else
    echo "Same amount of packets sent and received"
  fi

  end_streamer

  $VLBIPREFIX scripts/parse_logs $MAINLOG
  $VLBIPREFIX scripts/plot_parsed ${MAINLOG}.parsed -d -n -b -r -f $TARGETFOLDER &> /dev/null
  $VLBIPREFIX scripts/plot_parsed $STATLOG -c -m -f $TARGETFOLDER &> /dev/null
  sleep 1
  #rm ${TEMPLOG}${i}.parsed
  #rm ${TEMPLOG}${i}
  #rm $STATLOG

  echo -e "Testing $i ... \tOK"
done

end_streamer

echo "Cleaning up leftovers from previous test"
for i in ${METHODS[@]}
do
  $VLBIPREFIX "vbs_delete testfor_{i}"
done
